package com.xiang.publicdemo.module1.controller;

import com.xiang.publicdemo.common.utils.Console;
import com.xiang.publicdemo.module1.service.ITestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.session.Session;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

@Controller
@Slf4j
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedisOperationsSessionRepository sessionRepository;

    @Autowired
    private ITestService testService;

    @GetMapping("/test1")
    @ResponseBody
    public Object test1(HttpSession session){
        log.debug("sessionId:{}",session.getId());
        session.setAttribute("test","test1111");
        return null;
    }

    @GetMapping("/test2")
    @ResponseBody
    public Object test2(HttpSession session){
        log.debug("sessionId:{}",session.getId());
        session.invalidate();
        return null;
    }

    @GetMapping("/test3")
    @ResponseBody
    public Object test3(HttpSession session){
        Session redisSession = sessionRepository.findById(session.getId());
        log.debug("sessionId:{},httpsession test:{},redis session test {}: ",redisSession.getId(),session.getAttribute("test"),redisSession.getAttribute("test"));
        redisSession.setAttribute("test","bbbbb");
        //session.setAttribute("test","httpsession test...");
        log.debug("setAttribute    sessionId:{},httpsession test:{},redis session test {}: ",redisSession.getId(),session.getAttribute("test"),redisSession.getAttribute("test"));
        return null;
    }

    @GetMapping("/async")
    @ResponseBody
    public String async(HttpSession session){
        log.debug("sessionId:{}",session.getId());
        testService.async("aa");
        return "async test";
    }
}
